﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using MvcEngine.Core.Localization;
using MvcEngine.Core;
using MvcEngine.Core.Helpers;

namespace MvcEngine.Mvc.Controllers
{
    public class SubController : BaseController
    {
        protected override void Execute(RequestContext requestContext)
        {
            base.Execute(requestContext);
            this.ControllerContext.Controller.ValidateRequest = true;
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            
            //base.OnActionExecuting(filterContext);
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            LogHelper.Current.WriteError(filterContext.Exception.Message, filterContext.Exception);

            filterContext.ExceptionHandled = true;
            ControllerContext controllerContext = filterContext.Controller.ControllerContext;

            if (filterContext.Exception is PermissionException)
                controllerContext.RouteData.Values["viewName"] = "AccessForbidden";
            else if (filterContext.Exception is ModulePermissionException)
                controllerContext.RouteData.Values["viewName"] = "ModuleAccessForbidden";
            else
                controllerContext.RouteData.Values["viewName"] = "ModuleIsUnavailable";

            RedirectToErrorPage(controllerContext, "Error", "Error");
        }
    }
}
